home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / src / tclXclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-07  |  4.9 KB  |  187 lines  |  [TEXT/MPS ]

  1. #ifdef MPW
  2. #    pragma segment TCLExtend
  3. #endif
  4. /* 
  5.  * tclXclock.c --
  6.  *
  7.  *      Contains the TCL time and date related commands.
  8.  *-----------------------------------------------------------------------------
  9.  * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
  10.  *
  11.  * Permission to use, copy, modify, and distribute this software and its
  12.  * documentation for any purpose and without fee is hereby granted, provided
  13.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  14.  * Mark Diekhans make no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without express or
  16.  * implied warranty.
  17.  *-----------------------------------------------------------------------------
  18.  * $Id: tclXclock.c,v 2.7 1993/08/25 03:15:47 markd Exp $
  19.  *-----------------------------------------------------------------------------
  20.  */
  21.  
  22. #include "tclExtdInt.h"
  23.  
  24. #ifdef macintosh
  25.     /*
  26.     ** Difference between Mac and Unix times
  27.     */
  28. #    define TIMEDIFF    0x7c25b080
  29. #endif
  30.  
  31. /*
  32.  *-----------------------------------------------------------------------------
  33.  *
  34.  * Tcl_GetclockCmd --
  35.  *     Implements the TCL getclock command:
  36.  *         getclock
  37.  *
  38.  * Results:
  39.  *     Standard TCL results.
  40.  *
  41.  *-----------------------------------------------------------------------------
  42.  */
  43. int
  44. Tcl_GetclockCmd (clientData, interp, argc, argv)
  45.     ClientData  clientData;
  46.     Tcl_Interp *interp;
  47.     int         argc;
  48.     char      **argv;
  49. {
  50.     if (argc != 1) {
  51.         Tcl_AppendResult (interp, tclXWrongArgs, argv[0], (char *) NULL);
  52.         return TCL_ERROR;
  53.     }
  54. #ifdef macintosh
  55.     sprintf (interp->result, "%lu",
  56.                 ((unsigned long)time((time_t *) NULL) - TIMEDIFF) );
  57. #else
  58.     sprintf (interp->result, "%ld", time ((time_t *) NULL));
  59. #endif
  60.     return TCL_OK;
  61. }
  62.  
  63. /*
  64.  *-----------------------------------------------------------------------------
  65.  *
  66.  * Tcl_FmtclockCmd --
  67.  *     Implements the TCL fmtclock command:
  68.  *         fmtclock clockval ?format? ?GMT|{}?
  69.  *
  70.  * Results:
  71.  *     Standard TCL results.
  72.  *
  73.  *-----------------------------------------------------------------------------
  74.  */
  75.  
  76. int
  77. Tcl_FmtclockCmd (clientData, interp, argc, argv)
  78.     ClientData  clientData;
  79.     Tcl_Interp *interp;
  80.     int         argc;
  81.     char      **argv;
  82.     {
  83.     int              useGMT = FALSE;
  84.     unsigned long    clockVal;
  85.     char            *format;
  86.     struct tm       *timeDataPtr;
  87.     int              stat;
  88.     char            buffer[1024];
  89. #ifdef TCL_USE_TIMEZONE_VAR
  90.     int              savedTimeZone;
  91.     char            *savedTZEnv;
  92. #endif
  93.  
  94.     if ((argc < 2) || (argc > 4)) {
  95.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  96.                           " clockval ?format? ?GMT|{}?", (char *) NULL);
  97.         return TCL_ERROR;
  98.     }
  99.  
  100.     if (Tcl_GetLong (interp, argv[1], &clockVal) != TCL_OK)
  101.         {
  102.         Tcl_AppendResult(interp, "bad time value \"", argv[1], "\"", NULL);
  103.         return TCL_ERROR;
  104.         }
  105.     
  106.     if ((argc == 4) && (argv [3][0] != '\0')) {
  107.         if (!STREQU (argv [3], "GMT")) {
  108.             Tcl_AppendResult (interp, "expected \"GMT\" or {} got \"",
  109.                               argv [3], "\"", (char *) NULL);
  110.             return TCL_ERROR;
  111.         }
  112.         useGMT = TRUE;
  113.     }
  114.  
  115.     if ((argc >= 3) && (argv [2][0] != '\0'))
  116.         format = argv[2];
  117.     else
  118.         format = "%a %b %d %X %Z %Y";
  119.  
  120. #ifdef TCL_USE_TIMEZONE_VAR
  121.     /*
  122.      * This is a horrible kludge for systems not having the timezone in
  123.      * struct tm.  No matter what was specified, they use the global time
  124.      * zone.
  125.      */
  126.     if (useGMT) {
  127.         char *varValue;
  128.  
  129.         varValue = Tcl_GetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
  130.         if (varValue != NULL)
  131.             savedTZEnv = ckstrdup (varValue);
  132.         else
  133.             savedTZEnv = NULL;
  134.         Tcl_SetVar2 (interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
  135.         savedTimeZone = timezone;
  136.         timezone = 0;
  137.         tzset ();
  138.     }
  139. #endif
  140.  
  141. #ifdef macintosh
  142.     clockVal += TIMEDIFF;
  143. #endif
  144.  
  145.     if (useGMT)
  146.         {
  147. #ifdef macintosh
  148.         /* UNDONE UNDONE */
  149.         /* On the Mac, in MPW, gmtime() returns NULL!!! */
  150.         /* gmtime ((time_t *) &clockVal); */
  151.         timeDataPtr = localtime ((time_t *) &clockVal);
  152. #else
  153.         timeDataPtr = gmtime ((time_t *) &clockVal);
  154. #endif
  155.         }
  156.     else
  157.         {
  158.         timeDataPtr = localtime ((time_t *) &clockVal);
  159.         }
  160.  
  161.     stat = strftime (buffer, sizeof(buffer)-2, format, timeDataPtr);
  162.  
  163. #ifdef TCL_USE_TIMEZONE_VAR
  164.     if (useGMT) {
  165.         if (savedTZEnv != NULL) {
  166.             Tcl_SetVar2 (interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
  167.             ckfree (savedTZEnv);
  168.         } else {
  169.             Tcl_UnsetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
  170.         }
  171.         timezone = savedTimeZone;
  172.         tzset ();
  173.     }
  174. #endif
  175.  
  176.     if (stat <= 0) {
  177.         Tcl_AppendResult (interp, "error formatting time", (char *) NULL);
  178.         return TCL_ERROR;
  179.         }
  180.     else
  181.         {
  182.         Tcl_SetResult(interp, buffer, TCL_VOLATILE);
  183.         }
  184.         
  185.     return TCL_OK;
  186.     }
  187.